package com.jwthhealth_pub.old.diet.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.common.ImageLoader;
import com.jwthhealth_pub.old.diet.moudel.DietThiModule;
import com.jwthhealth_pub.old.diet.view.DietForActivity;

import java.util.List;

/**
 * Created by Administrator on 2016/12/27.
 */
public class DietforAdapter extends RecyclerView.Adapter {

    private Context mContext;
    DietThiModule.DataBean data;

    public DietforAdapter(DietForActivity dietForActivity, DietThiModule.DataBean dataBean) {
        mContext = dietForActivity;
        this.data = dataBean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == ITEM_TYPE.ITEM_HEAD.ordinal()) {
            return new HeadHolder(LayoutInflater.from(mContext).inflate(R.layout.item_diet_for_head, parent, false));
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_diet_for, parent, false);
            return new ViewHoder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (position == 0) {
            HeadHolder headHolder = (HeadHolder) holder;
            headHolder.title.setText(data.getName());
            headHolder.classcify.setText("分类: "+data.getTypename());
//            Picasso.with(mContext).load(data.getPic_path()).into(headHolder.imageView);
            ImageLoader.picasso(data.getPic_path(), headHolder.imageView);
        } else {
            int index = position - 1;
            DietThiModule.DataBean.DescBean descBean = data.getDesc().get(index);
            ViewHoder viewHoder = (ViewHoder) holder;
            viewHoder.title.setText(descBean.getName());
            String desc = "";
            List<String> tempDesc = descBean.getDesc();
            for (int i = 0; i< tempDesc.size(); i++){
                desc = desc + tempDesc.get(i) + "\n";
            }
            viewHoder.summary.setText(desc);
        }
    }

    @Override
    public int getItemCount() {
        return 1 + data.getDesc().size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_TYPE.ITEM_HEAD.ordinal();
        } else {
            return ITEM_TYPE.ITEM_BODY.ordinal();
        }
    }

    static class ViewHoder extends RecyclerView.ViewHolder{

        TextView title, summary;

        public ViewHoder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.head_title);
            summary = (TextView) itemView.findViewById(R.id.summary);
        }
    }

    static class HeadHolder extends RecyclerView.ViewHolder{

        private final ImageView imageView;
        private final TextView classcify;
        private final TextView title;

        public HeadHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.image);
            classcify = (TextView) itemView.findViewById(R.id.clissify);
            title = (TextView) itemView.findViewById(R.id.head_title);
        }
    }

    private enum ITEM_TYPE{
        ITEM_HEAD, ITEM_BODY;
    }
}
